package aaa.analyzer;

import java.util.ArrayList;
import java.util.Iterator;

import aaa.database.Database;
import aaa.schedule.Lesson;
import aaa.schedule.Schedule;

public class FeedAnalyzer {

	protected static final int ROOMS = 0 ;
	protected static final int GROUPS = 1 ;
	protected static final int TEACHERS = 2 ;
	protected static final int TIME = 3 ;

	protected static final int UNKNOWN = 0 ;
	protected static final int IS_TIME = 1 ;
	protected static final int IS_GROUP = 2 ;
	protected static final int IS_TEACHER = 3 ;
	protected static final int IS_ROOM = 4 ;

	protected Schedule schedule ;
	protected ArrayList<Feed> feeds ;
	protected Database db ;

	public FeedAnalyzer (Schedule schedule, ArrayList<Feed> feeds, Database db) {
		this.schedule = schedule ;
		this.feeds = feeds ;
		this.db = db ;
	}// FeedPlayer(Schedule)

	/**
	 * Analyzes the content of the feeds and builds the Lessons.
	 */
	public void start () {
		this.schedule.ensureCapacity(this.feeds.size()) ;
		for(Feed feed : this.feeds) {
			Lesson l = new Lesson() ;
			//TODO Generate a lesson id -- Default one for the moment
			l.setLessonId(feed.getId()) ;
			l.setTitle(feed.getTitle()) ;
			l.setSchedule(this.schedule.getName()) ;
			l.setType(0);
			l.setCreated(feed.getPubDate());
			// Analyze the description
			String[] description = this.analyzeDescription(feed) ;
			l.setRoom(description[ROOMS]);
			l.setGroups(description[GROUPS]);
			l.setTeachers(description[TEACHERS]);
			l.setTime(insertDate(description[TIME])) ;
			// Add the lesson
			this.schedule.append(l) ;
		}// for
	}// start()

	protected static String insertDate (String dateTime) {
		int posSpace = dateTime.indexOf(" ") ;
		String datePart = new String(dateTime.substring(0, posSpace)) ;
		String[] parts = datePart.split("/") ;
		StringBuilder invDate = new StringBuilder() ;
		invDate.append(parts[2]) ;
		invDate.append("/") ;
		invDate.append(parts[1]) ;
		invDate.append("/") ;
		invDate.append(parts[0]) ;
		invDate.append(new String(dateTime.substring(posSpace))) ;
		return invDate.toString() ;
	}
	
	protected String[] analyzeDescription(Feed analyzedFeed) {
		String lessonTitle = analyzedFeed.getTitle() ;
		ArrayList<String> tparts = new ArrayList<String>() ;
		ArrayList<Integer> tstat = new ArrayList<Integer>() ;
		for(String part : analyzedFeed.getDescription().split("(<p>)|(<p></p>)|(</p>)|(<b>Resources</b>)|(<b>Ressources</b>)|(<br />)|(<br/>)")) {
			if (!part.equals("") && !part.equals(lessonTitle)) {
				tparts.add(part) ;
				tstat.add(0) ;
			}// if
		}// for
		String[] properties = new String[4] ;
		
		// Retrieve time, it is always in first position
		properties[TIME] = tparts.get(0) ;
		tstat.set(0, IS_TIME) ;
		
		// Retrieve the rooms, using the database
		Iterator<Integer> status = tstat.iterator()  ;
		Iterator<String> parts   = tparts.iterator() ;
		ArrayList<String> potentialRooms = new ArrayList<String>() ;
		parts.next() ;
		while (parts.hasNext()) {
			String part = parts.next() ;
			potentialRooms.add(part) ;
		}// while
		ArrayList<String> rooms = this.db.getRooms(potentialRooms) ;
		StringBuffer foundRooms = new StringBuffer() ;
		for(String room : rooms) {
			int pos = tparts.indexOf(room) ;
			tstat.set(pos, IS_ROOM) ;
			foundRooms.append(", ") ;
			foundRooms.append(room) ;
		}// for
		if (foundRooms.length() == 0) {
			foundRooms.append(", Inconnu") ;
		}// if
		properties[ROOMS] = foundRooms.substring(2) ;
		
		// Retrieve the teachers
		boolean teacherFound = false ;
		StringBuffer foundTeachers = new StringBuffer() ;
		int pos = tparts.size()-1 ;
		while (pos > 0) {
			if (tstat.get(pos) == UNKNOWN) {
				String part = tparts.get(pos); 
				if (part.matches("[\\p{javaUpperCase} \\-']+")) {
					teacherFound = true ;
					tstat.set(pos, IS_TEACHER) ;
					foundTeachers.append(", ") ;
					foundTeachers.append(part) ;
				} else if (teacherFound) {
					break ;// All teachers have been found
				}// if
			}// if
			pos-- ;
		}// while
		if (foundTeachers.length() == 0) {
			foundTeachers.append(", Inconnu") ;
		}// if
		properties[TEACHERS] = foundTeachers.substring(2) ;
		
		// Retrieve the groups, they should be what is left
		StringBuffer foundGroups = new StringBuffer() ;
		status = tstat.iterator()  ;
		parts   = tparts.iterator() ;
		while (parts.hasNext()) {
			String part = parts.next() ;
			if (status.next() == UNKNOWN) {
				foundGroups.append(", ") ; 
				foundGroups.append(part) ;
			}// if
		}// while
		if (foundGroups.length() == 0) {
			foundGroups.append(", Inconnu") ;
		}// if
		properties[GROUPS] = foundGroups.substring(2) ;
		
		return properties ;
	}// analyzeDescription(String)

}// class FeedPplayer